import Vue from 'vue'
import { getDictDetail } from '@/api/dict'

export default class Dict {
  constructor(dict) {
    this.dict = dict
  }

  async init(names, completeCallback) {
    if (names === undefined || name === null) {
      throw new Error('need Dict names')
    }
    const ps = []
    names.forEach((n) => {
      Vue.set(this.dict.dict, n, {})
      Vue.set(this.dict.label, n, {})
      Vue.set(this.dict, n, [])
      ps.push(
        getDictDetail(n).then((res) => {
          /* 
          结构示例 hide_status
          this.dict = {
            dict: { hide_status: { '0': { id: 2, dictId: 1, value: '0', label: '显示', seq: 1 }, '1': { id: 1, dictId: 1, value: '1', label: '隐藏', seq: 0 } } },
            label: { hide_status: { '0': '显示', '1': '隐藏' } },
            hide_status: [
              { id: 1, dictId: 1, value: '1', label: '隐藏', seq: 0 },
              { id: 2, dictId: 1, value: '0', label: '显示', seq: 1 }
            ]
          } 
          */
          this.dict[n].splice(0, 0, ...res.data)
          res.data.forEach((d) => {
            Vue.set(this.dict.dict[n], d.code, d)
            Vue.set(this.dict.label[n], d.code, d.label)
          })
        })
      )
    })
    await Promise.all(ps)
    completeCallback()
  }
}
